package me.may.money.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedHashMap;

import org.bukkit.Bukkit;

import com.google.common.collect.Maps;

/**
 * 交子DAO层
 * 
 * @author May_Speed
 */
public class MoneyDao {
	
	public static void setPlayerMoney(String name, String amount) {
		Connection conn = null;
		PreparedStatement ps = null;		
		String sql = "UPDATE playerMoney SET money = " + amount + " WHERE playerName = '" + name + "'";
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
			Bukkit.getConsoleSender().sendMessage("§e[行为记录]§f玩家 " + name + " 设置了 §a" + amount + " §f两交子");
			CacheDao.putPlayerData(name, getPlayerMoney(name));
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
	}
	
	public static void takePlayerMoney(String name, String amount) {
		Connection conn = null;
		PreparedStatement ps = null;		
		String sql = "UPDATE playerMoney SET money = money - " + amount + " WHERE playerName = '" + name + "'";
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
			Bukkit.getConsoleSender().sendMessage("§e[行为记录]§f玩家 " + name + " 支付了 §a" + amount + " §f两交子");
			CacheDao.putPlayerData(name, getPlayerMoney(name));
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
	}
	
	public static void addPlayerMoney(String name, String amount) {
		Connection conn = null;
		PreparedStatement ps = null;		
		String sql = "UPDATE playerMoney SET money = money + " + amount + " WHERE playerName = '" + name + "'";
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
			Bukkit.getConsoleSender().sendMessage("§e[行为记录]§f玩家 " + name + " 增加了 §a" + amount + " §f两交子");
			CacheDao.putPlayerData(name, getPlayerMoney(name));
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
	}
	
	public static void initialPlayerData(String name) {
		Connection conn = null;
		PreparedStatement ps = null;		
		String sql = "INSERT INTO playerMoney VALUES(null, '" + name + "', '0')";
		
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
			CacheDao.putPlayerData(name, "0");
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
	}
	
	public static boolean playerHasData(String name) {
		Connection conn = null;
		PreparedStatement ps = null;		
		ResultSet rs = null;
		String sql = "SELECT playerName FROM playerMoney WHERE playerName = '" + name + "'";
		
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next()) {
				return true;
			}
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
		return false;
	}
	
	public static String getPlayerMoney(String name) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String result = null;
		String sql = "SELECT money FROM playerMoney WHERE playerName = '" + name + "'";
		
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			if(rs.next()){
				result = rs.getString(1);
			}
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
		return result;
	}
	
	public static LinkedHashMap<String, Double> getTop(int limit) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		LinkedHashMap<String, Double> map = Maps.newLinkedHashMap();
		String sql = "SELECT playerName, money FROM playerMoney ORDER BY money DESC LIMIT " + limit;
		try {
			conn = BasicDao.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			while(rs.next()) {
				String name = rs.getString("playerName");
				double money = rs.getDouble("money");
				map.put(name, money);
			}
			
		} catch (Exception e) {
			System.out.println("错误: " + e.getMessage());
		}
		return map;
	}
}
